'use strict';

var express = require('express');
var httpProxy = require('http-proxy');

//创建express实例
var app = express();
var port = 5050;

app.use(express.static('app'));
app.use(express.static('bower_components'));
app.use('/bower_components', express.static('bower_components'));
app.use(express.static('.tmp'));

var production = {
	host: 'api.xianwan.io'
}

var test = {
	host: '172.16.1.25',
	port: 8081
}

var development = {
	host: '120.236.169.14',
	port: 7081
}

var proxy = httpProxy.createProxyServer({
	target: development
});

//index page
app.get('/', function (req, res) {
	res.redirect('/app/index.html');
});

// 代理中间件，只代理url包含/webservice的请求
app.use(function (req, res, next) {
	if (req.url.indexOf('.') == -1 || req.url.indexOf('access_token') > -1) {
		console.log('proxy to: api.xianwan.io', req.url);
		proxy.web(req, res);
	} else {
		next();
	}
});

app.listen(process.env.PORT || port);

console.log('xianwan Exhibition, Show Time!');
console.log('Listening Port:' + port);